package samsung.sds.business.vdiManagementSystem.firewallManagement.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.business.vdiManagementSystem.firewallManagement.model.FirewallEntity;
import samsung.sds.business.vdiManagementSystem.firewallManagement.service.FirewallManagementService;
import samsung.sds.business.vdiManagementSystem.vdiManagement.model.VDIInfoEntity;
import samsung.sds.business.vdiManagementSystem.vdiManagement.service.VDIManagementService;
import samsung.sds.departmentManagement.model.DepartmentEntity;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
import samsung.sds.userManagement.model.UserEntity;
import samsung.sds.userManagement.service.UserManagementService;


/**
 * @author hongxin.luo
 * @createDate 2016-04-21
 *
 * 防火墙管理控制器
 */
@Controller
@RequestMapping("/firewallManagement")
public class FirewallManagementController {
	@Autowired
	private FirewallManagementService firewallManagementServiceImpl;
	
	@Autowired
	private UserManagementService userManagementServiceImpl;
	@Autowired
	private VDIManagementService vdiManagementServiceImpl;
	
	/**
	 * 跳转到防火墙管理页面
	 * @param request,modelMap
	 */
	@RequestMapping(value="/firewallList",method = RequestMethod.POST)
	public String firewallList(HttpServletRequest request,ModelMap modelMap){
		String treeNodeId = "" ;
		if (request.getParameter("treeNodeId") != null && !"".equals(request.getParameter("treeNodeId"))) {
			treeNodeId = request.getParameter("treeNodeId"); 
			modelMap.put("treeNodeId", treeNodeId );
		}		
		return "/business/vdiManagementSystem/firewallManagement/firewallList";
	}
	
	/**
	 * 查询组织结构以及各部门的VDI信息
	 * @param
	 */
	@RequestMapping(value="/queryDepartmentVDI",method = RequestMethod.POST)
	@ResponseBody
	public List<DepartmentEntity> queryDepartmentVDI(){
		
		List<DepartmentEntity> departmentVDIs = new ArrayList<DepartmentEntity>();
		
		departmentVDIs = firewallManagementServiceImpl.queryDepartmentVDI();
		
		return departmentVDIs;
	}
		
	/**
	 * 查询VDI对应的防火墙管理页面
	 * @param request
	 */
	@RequestMapping(value="/queryDeptVDIFirewall",method = RequestMethod.POST)
	@ResponseBody
	public PageData<FirewallEntity> queryDeptVDIFirewall(HttpServletRequest request){
		QueryParam param = CommonTools.getQueryParam(request);
		return firewallManagementServiceImpl.queryFirewallInVDI(param);
	}
	
	/**
	 * 从VDI中删除防火墙信息
	 * @param request
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/removeFirewallsFromVDI",method = RequestMethod.POST)
	@ResponseBody
	public String removeFirewallsFromVDI(HttpServletRequest request) throws UnsupportedEncodingException {
		// 从VDI中移除防火墙信息
		String firewallStr = request.getParameter("firewalls");
		String[] firewalls = firewallStr.split(",");
		String lastModifyUser = CommonTools.getSessionUserName();
		int removeCount = firewallManagementServiceImpl.updateFirewall(lastModifyUser,firewalls);
		if( removeCount > 0 ){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	 * 弹出禀议模板
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/firewallApply")
	public String toFirewallApply(ModelMap modelMap){
		String username = CommonTools.getSessionUserName();
		UserEntity user = new UserEntity();
		user.setUsername(username);
		user = this.userManagementServiceImpl.findUniqueByProperty(user);
		
		modelMap.addAttribute("user", user);
		return "/business/vdiManagementSystem/firewallManagement/firewallApply";
	}
	
	/**
	 * insert new FireWall info.
	 * @param modelMap,request
	 * @return
	 */
	@RequestMapping(value="/showAddView")
	public String showAddOrUpdateFireWallView(ModelMap modelMap,HttpServletRequest request) {
		
		if (request.getParameter("treeNodeId") !=null && !"".equals(request.getParameter("treeNodeId"))) {
			modelMap.put("treeNodeId", request.getParameter("treeNodeId"));
		}
		if(request.getParameter("fireWallID") !=null && !"".equals(request.getParameter("fireWallID"))) {
			modelMap.put("op", "editOne");
			modelMap.put("fireWallID", request.getParameter("fireWallID"));		
		}
		
		return "/business/vdiManagementSystem/firewallManagement/addOrUpdateFireWallInfo";
	}
	
	@RequestMapping(value="/getTreeData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, List<?>> getTreeData(ModelMap modelMap) {
		return firewallManagementServiceImpl.getTreeData();
	}
	
	@RequestMapping(value="/getFireWallDetail")
	@ResponseBody
	public FirewallEntity getFireWallDetail(ModelMap modelMap, String id) {
		FirewallEntity fe = firewallManagementServiceImpl.getFireWallByID(id);
		
		return fe;
	} 
	
	@RequestMapping(value="/addFireWallList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addFireWallList(ModelMap modelMap, @RequestBody List<FirewallEntity> fireWallList) {
		Map<String, String> resultMap = new HashMap<String, String>();
		
		try{
			firewallManagementServiceImpl.batchInserFireWall(fireWallList);
			resultMap.put("result", "1");
		}
		catch(Exception e) {
			resultMap.put("result", null);
		}
		
		return resultMap;
	}
	
	@RequestMapping(value="/initFirewallList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> initFirewallList(ModelMap modelMap,String vdiId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try{
			List<FirewallEntity> list = firewallManagementServiceImpl.queryFirewallEntityByVDIId(vdiId);
			
			VDIInfoEntity vdiInfo_temp = new VDIInfoEntity();
			vdiInfo_temp.setId(vdiId);
			vdiInfo_temp = vdiManagementServiceImpl.queryVdiListById(vdiInfo_temp);
			
			resultMap.put("result", list);
			resultMap.put("vdiInfo", vdiInfo_temp);
		}
		catch(Exception e) {
			e.printStackTrace();
			resultMap.put("result", null);
			resultMap.put("vdiInfo", null);
		}
		
		return resultMap;
	}
	
	@RequestMapping(value="/updateFireWallOne", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> updateFireWallOne(ModelMap modelMap, @RequestBody FirewallEntity editOne){
		System.err.println("----updateOne");
		System.err.println(editOne);
		Map<String, String> resultMap = new HashMap<String, String>();
		
		try{
			firewallManagementServiceImpl.updateFirewallEntity(editOne);
			resultMap.put("result", 1 + "");
		}
		catch(Exception e) {
			e.printStackTrace();
			
			resultMap.put("result", 0 + "");
		}
		
		return resultMap;
	}
	
	@RequestMapping(value="/batchUpdateFireWallList")
	@ResponseBody
	public Map<String, String> batchUpdateFireWallList(ModelMap modelMap, @RequestBody List<FirewallEntity> fireWallList, String backUpEditFireWallIDList){
		Map<String, String> resultMap = new HashMap<String, String>();
		
		String[] backUp_temp = null;
		if("".equals(backUpEditFireWallIDList)) {
			backUp_temp = new String[0];
		}
		else {
			backUp_temp = backUpEditFireWallIDList.split(",");
		}
		
		List<FirewallEntity> modifyList = new ArrayList<FirewallEntity>();
		List<String> deleteList = new ArrayList<String>();
		List<FirewallEntity> addList = new ArrayList<FirewallEntity>();
		String deleteList_temp = "";
		
		System.err.println("fireWallList");
		System.err.println(fireWallList);
		
		// process insert condition.
		for(int i = 0; i < fireWallList.size(); i++) {
			if("".equals(fireWallList.get(i).getId())) {
				addList.add(fireWallList.get(i));
			}
		}
		
		for(int i = 0; i < backUp_temp.length; i++) {
			boolean flag = false;
			
			for(int j = 0; j < fireWallList.size(); j++) {
				if(backUp_temp[i].equals(fireWallList.get(j).getId())) {
					modifyList.add(fireWallList.get(j));
					flag = true;
					
					break;
				}
			}
			
			if(flag) {
				continue;
			}
			
			deleteList_temp += backUp_temp[i] + ",";
			deleteList.add(backUp_temp[i]);
		}
		
		if(deleteList.size() > 0) {
			deleteList_temp.substring(0, deleteList_temp.length() - 1);
		}
		
		try{
			if(modifyList.size() > 0) {
				firewallManagementServiceImpl.batchUpdateFirewallEntity(modifyList);
			}
			
			if(addList.size() > 0) {
				for(int i = 0; i < addList.size(); i++) {
					firewallManagementServiceImpl.insertFirewallInfo(addList.get(i));
				}
			}
			
			if(deleteList.size() > 0) {
				firewallManagementServiceImpl.updateFirewall(CommonTools.getSessionUserName(), deleteList_temp.split(","));
			}
			
			resultMap.put("result", "1");
		}
		catch(Exception e) {
			e.printStackTrace();
			resultMap.put("result", null);
		}
		
		return resultMap;
	}
	
	/**
	 * 跳转到VDI详情Tab中防火墙信息页面
	 * @param modelMap
	 * @param vdiIp
	 * @param op
	 * @return
	 */
	@RequestMapping(value="/toFirewallTab_sitemesh_popup")
	public String toFirewallTab(ModelMap modelMap,String vdiId,String op){
		modelMap.put("op", "edit");
		modelMap.put("vdiId", vdiId);
		
		return "/business/vdiManagementSystem/firewallManagement/addOrUpdateFireWallInfo";
	}
}
